package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.consts.ErmsCodePrefix;
import com.sugon.dao.SysRoleDao;
import com.sugon.entity.SysRoleEntity;
import com.sugon.service.SysRoleDeptService;
import com.sugon.service.SysRoleMenuService;
import com.sugon.service.SysRoleService;
import com.sugon.service.SysUserService;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 角色
 *
 * @author lipengjun
 * @date 2016年9月18日 上午9:45:12
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;

    @Override
    public SysRoleEntity queryObject(String roleId) {
        return sysRoleDao.queryObject(roleId);
    }

    @Override
    public List<SysRoleEntity> queryList(Map<String, Object> map) {
        return sysRoleDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysRoleDao.queryTotal(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysRoleEntity role) {
        role.setCreateTime(DateUtil.date());
        role.setCreateUser(ShiroUtils.getUserId());
        role.setRoleId(RandomUtil.randomString(32));
        role.setRoleCode(getRoleCode());
        sysRoleDao.save(role);
        //检查权限是否越权
//        checkPrems(role);
        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleEntity role) {
        role.setUpdateTime(DateUtil.date());
        role.setUpdateUser(ShiroUtils.getUserId());
        // 如果角色编码未设置，那么回填
        if (StrUtil.isEmpty(role.getRoleCode())) {
            role.setRoleCode(getRoleCode());
        }
        sysRoleDao.update(role);
        //检查权限是否越权
//        checkPrems(role);
        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

    @Override
    @Transactional
    public void deleteBatch(String[] roleIds) {
        sysRoleDao.deleteBatch(roleIds);
    }

    /**
     * 获取角色CODE
     * 从已存在的角色中找出最新的角色CODE,再这个基础上加1
     *
     * @return
     */
    private String getRoleCode() {
        int num = 1;
        String deptCode = sysRoleDao.getLastestRoldCode();
        if (StrUtil.isNotEmpty(deptCode)) {
            num += Integer.parseInt(deptCode.substring(2, 4));
        }
        return ErmsCodePrefix.ROLE_CODE_PREFIX + String.format("%02d", num);
    }
}
